package com.styytech.dadabao.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.styytech.dadabao.R;
import com.styytech.dadabao.bean.RecommendProduct;
import com.styytech.dadabao.ui.project.ProjectDetailsActivity;
import com.styytech.dadabao.widge.progressbar.roundprogressbar.RoundProgressBar;

import java.util.LinkedList;

/**
 * Created by user on 2015/8/14.
 */
public class ProductItemAdapter extends BaseAdapter {

    private LinkedList<Object> mList = null;
    private LayoutInflater mInflater = null;
    ViewHolder holder = null;
    private Context mContext;

    public ProductItemAdapter(LinkedList<Object> mList, Context context) {
        // TODO Auto-generated constructor stub
        this.mList = mList;
        this.mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.adapter_product_item, null);
            holder.tv_pro_name = (TextView) convertView.findViewById(R.id.tv_pro_name);
            holder.tv_expected_return = (TextView) convertView.findViewById(R.id.tv_expected_return);
            holder.tv_pro_time_limit = (TextView) convertView.findViewById(R.id.tv_pro_time_limit);
            holder.tv_pro_people_num = (TextView) convertView.findViewById(R.id.tv_pro_people_num);
            holder.tv_pro_scale = (TextView) convertView.findViewById(R.id.tv_pro_scale);
            holder.roundProgressBar = (RoundProgressBar) convertView.findViewById(R.id.roundProgressBar);
            holder.tv_pro_progress = (TextView) convertView.findViewById(R.id.tv_pro_progress);
            holder.llyt_one=(LinearLayout)convertView.findViewById(R.id.llyt_one);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        WindowManager m = ((Activity)mContext).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        LinearLayout.LayoutParams sp_params = new LinearLayout.LayoutParams(

                AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);

        sp_params.width = (int) (d.getWidth() * 0.5);
        holder.llyt_one.setLayoutParams(sp_params);
        RecommendProduct productItem = (RecommendProduct) mList.get(position);

        //项目名称
        holder.tv_pro_name.setText(productItem.getProductName());
        //项目收益
        double ssss=Double.parseDouble(productItem.getExpectedReturnRatio());
        holder.tv_expected_return.setText(String.format("%.2f",ssss) + "%");
        //项目期限
        holder.tv_pro_time_limit.setText(productItem.getTimeLimit() + "天");
        //已投资人数
        holder.tv_pro_people_num.setText(productItem.getPeople_num() + "人");
        //项目规模
        if(productItem.getTotalCredit()<10000){
            holder.tv_pro_scale.setText((int)productItem.getTotalCredit() + "元");
        }else{
            holder.tv_pro_scale.setText((int)(productItem.getTotalCredit() / 10000 )+ "万元");
        }

        //进度百分比
        holder.roundProgressBar.setFirst_start(-90);
        holder.roundProgressBar.setTotal_length(360);
        String ss=String.format("%.2f", ((float) productItem.getProgress() / (float) productItem.getTotalCredit()) * 100);
        int s = (int) (((float) productItem.getProgress() / (float) productItem.getTotalCredit()) * 100);
        int dueStatus=Integer.parseInt(productItem.getDueStatus());
        int saleStatus=Integer.parseInt(productItem.getSaleStatus());

        if(dueStatus==0){
            holder.llyt_one.setBackgroundColor(mContext.getResources().getColor(R.color.gray_dark));
            holder.tv_pro_progress.setText("已结束");
            holder.roundProgressBar.setProgress(0);
        }/*else if (saleStatus==0){
            holder.llyt_one.setBackgroundColor(mContext.getResources().getColor(R.color.gray_dark));
            holder.tv_pro_progress.setText("预售");
            holder.roundProgressBar.setProgress(0);
        }*/
        else if (saleStatus==2){
            holder.llyt_one.setBackgroundColor(mContext.getResources().getColor(R.color.gray_dark));
            holder.tv_pro_progress.setText("停售");
            holder.roundProgressBar.setProgress(0);
        } else{
            if(s==100){//完成
                holder.llyt_one.setBackgroundColor(mContext.getResources().getColor(R.color.gray_dark));
                holder.tv_pro_progress.setText(mContext.getResources().getString(R.string.project_full));
                holder.roundProgressBar.setProgress(s);
            }else {
                holder.llyt_one.setBackgroundColor(mContext.getResources().getColor(R.color.main));
                holder.tv_pro_progress.setText(ss + "%");
                holder.roundProgressBar.setProgress(s);
            }
        }


        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //点击单个条目
                Intent intent = new Intent();
                intent.setClass(mContext, ProjectDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", ((RecommendProduct) mList.get(position)).getId());
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });


        return convertView;
    }

    private class ViewHolder {
        //项目名称
        private TextView tv_pro_name;
        //项目收益
        private TextView tv_expected_return;
        //项目期限
        private TextView tv_pro_time_limit;
        //已投资人数
        private TextView tv_pro_people_num;
        //项目规范
        private TextView tv_pro_scale;
        //进度条
        private RoundProgressBar roundProgressBar;
        //进度数字
        private TextView tv_pro_progress;
        private LinearLayout llyt_one;

    }
}
